package com.kcwl.carrier.infrastructure.repository.persostence;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kcwl.carrier.domain.condition.CarrierMapCondition;
import com.kcwl.carrier.domain.entity.CarrierMappingInfo;
import com.kcwl.carrier.domain.repository.ICarrierMappingRepository;
import com.kcwl.carrier.infrastructure.repository.mapper.CarrierMappingMapper;
import com.kcwl.carrier.infrastructure.repository.po.CarrierMappingPO;
import com.kcwl.carrier.interfaces.query.CarrierListQuery;
import com.kcwl.ddd.application.constants.YesNoEnum;
import com.kcwl.ddd.domain.entity.KcPage;
import com.kcwl.ddd.domain.repository.BaseRepositoryImpl;
import com.kcwl.framework.utils.CollectionUtil;
import com.kcwl.framework.utils.KcBeanConverter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author 刘旭超
 * @date 2022年07月11日 16:33
 * @Description
 */
@Service
public class CarrierMappingRepositoryImpl extends BaseRepositoryImpl<CarrierMappingMapper, CarrierMappingPO> implements ICarrierMappingRepository {

    @Resource
    private CarrierMappingMapper carrierMapping;


    @Override
    public Class<CarrierMappingPO> getPoClass() {
        return CarrierMappingPO.class;
    }

    @Override
    public List<CarrierMappingInfo> getMappingList(CarrierMappingInfo vehicleCon) {
        QueryWrapper<CarrierMappingPO> wrapper = new QueryWrapper();
        wrapper.eq(Objects.nonNull(vehicleCon.getDriverId()), "driver_id", vehicleCon.getDriverId());
        wrapper.eq(Objects.nonNull(vehicleCon.getCarrierId()), "carrier_id", vehicleCon.getCarrierId());
        wrapper.eq(Objects.nonNull(vehicleCon.getVehicleId()), "vehicle_id", vehicleCon.getVehicleId());
        wrapper.orderByDesc("create_time");
        return KcBeanConverter.toList(carrierMapping.selectList(wrapper), CarrierMappingInfo.class);
    }

    @Override
    public void deleteByVehicleId(Long vehicleId) {
        UpdateWrapper<CarrierMappingPO> wrapper = new UpdateWrapper<>();
        wrapper.eq("vehicle_id", vehicleId);
        CarrierMappingPO po = new CarrierMappingPO();
        po.setIsDel(YesNoEnum.YEA.getValue());
        po.setUpdateTime(new Date());
        carrierMapping.update(po, wrapper);
    }

    @Override
    public CarrierMappingPO getDriverByVehicleId(Long vehicleId, Long driverId) {
        QueryWrapper<CarrierMappingPO> wrapper = new QueryWrapper<>();
        wrapper.eq("driver_id", driverId);
        wrapper.eq("vehicle_id", vehicleId);
        return carrierMapping.selectOne(wrapper);
    }

    @Override
    public Integer updateByVehicleId(CarrierMappingPO po) {
        UpdateWrapper<CarrierMappingPO> wrapper = new UpdateWrapper<>();
        wrapper.eq("vehicle_id", po.getVehicleId());
        wrapper.set("driver_id", po.getDriverId());
        return carrierMapping.update(po, wrapper);
    }

    @Override
    public List<CarrierMappingInfo> queryCarrierMappingInfo(CarrierMappingInfo carrierMappingInfo) {
        QueryWrapper<CarrierMappingPO> queryWrapper = new QueryWrapper<>();
        if (carrierMappingInfo.getId() != null) {
            queryWrapper.eq("id", carrierMappingInfo.getId());
        }
        if (carrierMappingInfo.getCarrierId() != null) {
            queryWrapper.eq("carrier_id", carrierMappingInfo.getCarrierId());
        }
        if (carrierMappingInfo.getDriverId() != null) {
            queryWrapper.eq("driver_id", carrierMappingInfo.getDriverId());
        }
        if (carrierMappingInfo.getVehicleId() != null) {
            queryWrapper.eq("vehicle_id", carrierMappingInfo.getVehicleId());
        }
        if (carrierMappingInfo.getIsDel() != null) {
            queryWrapper.eq("is_del", carrierMappingInfo.getIsDel());
        }
        return KcBeanConverter.toList(carrierMapping.selectList(queryWrapper), CarrierMappingInfo.class);
    }

    @Override
    public List<CarrierMappingPO> queryCarrierMappingInfoList(List<Long> vehicleIdList) {
        if (!CollectionUtil.isEmpty(vehicleIdList) && vehicleIdList.size() > 0) {
            QueryWrapper<CarrierMappingPO> wrapper = new QueryWrapper<>();
            wrapper.in("vehicle_id", vehicleIdList);
            return carrierMapping.selectList(wrapper);
        }
        return null;
    }

    @Override
    public void carrierUnbind(Long id) {
        carrierMapping.carrierUnbind(id);
    }

    /**
     * 根据车辆相关条件和用户id查询映射信息
     */
    @Override
    public List<CarrierMappingPO> queryByVehicleParamAndUser(CarrierListQuery query) {
        return carrierMapping.queryByVehicleParamAndUser(query);
    }

    /**
     * 根据司机相关条件和用户id查询映射信息
     */
    @Override
    public List<CarrierMappingPO> queryByCarrierParamAndUser(CarrierListQuery query) {
        return carrierMapping.queryByCarrierParamAndUser(query);
    }

    @Override
    public KcPage<CarrierMappingInfo> queryMappingPageByCondition(CarrierMapCondition condition) {
        IPage<CarrierMappingPO> iPage = carrierMapping.queryMappingPageByCondition(new Page<>(condition.getCurPagerNo(), condition.getPageSize()),condition);
        return new KcPage<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }
}
